import unittest
import requests
import os
from testvlog import vlogin

#登录-》获取登录昵称-》新建文章-》查看文章（会生成ID）-》删除新增的文章（通过ID删除）
class MyTestCase(unittest.TestCase):
    #全局变量
    global head,artid
    head = {"Cookie": "JSESSIONID="+vlogin.testlogin()}
    def test_01(self):
        '''获取用户昵称'''
        r = requests.get("http://124.222.181.200:8081/currentUserName", headers=head, timeout=2)
        if r.status_code == 200:
           self.assertEqual(r.text,"乔峰1111111")
           print(r.text)
        else:
            raise Exception("服务器请求异常:%s" % r.status_code)

    def test_03(self):
        ''' 查询已发表文章'''
        r = requests.get("http://124.222.181.200:8081/article/all?state=1&page=1&count=6&keywords=", headers=head,timeout=2)
        art=r.json()
        #获取添加的最新的文章ID
        artic=art['articles'][0]['id']
        print(artic)
        #globals() 函数 artid当做变量名字key值
        globals()['artid'] = artic

    #删除文章
    def test_04(self):
        dus={"aids":artid,"state": 1}
        r=requests.put("http://124.222.181.200:8081/article/dustbin",headers=head,data=dus,timeout=2)
        if r.status_code == 200:
            print(r.json())
            print(artid)
            self.assertEqual(r.json()['msg'], "删除成功!")
        else:
            raise RuntimeError("服务器请求异常:%s"%r.status_code)

if __name__ == '__main__':
  unittest.main()
